using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UI_SkillSlot : MonoBehaviour
{
    private Skill_DataSO skillData;
    private Image skillIcon;

    public SkillType skillType;
    [SerializeField] private Image skillCooldownMask;
    [SerializeField] private string inputCooldownName;
    [SerializeField] private TextMeshProUGUI inputkeyInfo;
    [SerializeField] private TextMeshProUGUI cooldownTimer;
    float timer;
    private void Awake()
    {
        skillIcon = GetComponent<Image>();
    }
    private void OnValidate()
    {
        gameObject.name = "UI_SkillSlot - " + skillType;
    }
    public void SetSkillSlot(Skill_DataSO skillData)
    {
        this.skillData = skillData;

        cooldownTimer.gameObject.SetActive(false);
        inputkeyInfo.text = inputCooldownName;
        skillIcon.sprite = skillData.icon;
    }
    public void StartCooldownInSlot()
    {
        skillCooldownMask.fillAmount = 1f;
        StartCoroutine(nameof(CooldownCo));
    }
    private IEnumerator CooldownCo()
    {
        float cooldownTime = skillData.skillUpgradeData.coolDown;
        timer = 0;
        cooldownTimer.gameObject.SetActive(true);
        while (timer <= cooldownTime)
        {
            timer += Time.deltaTime;
            skillCooldownMask.fillAmount = 1 - timer / cooldownTime;
            cooldownTimer.text = (cooldownTime - timer).ToString("F1");
            yield return null;
        }
        cooldownTimer.gameObject.SetActive(false);
    }

    public void UpdateCooldownReducing(float reducingTime) => timer += reducingTime;
}
